package com.wqr.mobile.android.ui.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * util for sharedpreferens
 * @author wenqiurong
 *
 */
public class SharedPrefsUtil {

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, int value) {
		Editor sp = getEditor(context);
		sp.putInt(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, boolean value) {
		Editor sp = getEditor(context);
		sp.putBoolean(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putValue(Context context, String key, String value) {
		Editor sp = getEditor(context);
		sp.putString(key, value);
		valueCommit(sp);
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static int getValue(Context context, String key, int defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		int value = sp.getInt(key, defValue);
		return value;
	}


	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static boolean getValue(Context context, String key, boolean defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		boolean value = sp.getBoolean(key, defValue);
		return value;
	}

	/**
	 * 
	 * @param context
	 * @param key
	 * @param defValue
	 * @return
	 */
	public static String getValue(Context context, String key, String defValue) {
		SharedPreferences sp = getSharedPreferences(context);
		String value = sp.getString(key, defValue);
		return value;
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static Editor getEditor(Context context) {
		return getSharedPreferences(context).edit();
	}

	/**
	 * 
	 * @param context
	 * @return
	 */
	private static SharedPreferences getSharedPreferences(Context context) {
		return context.getSharedPreferences("myui", Context.MODE_PRIVATE);
	}

	/**
	 * 
	 * @param sp
	 */
	private static void valueCommit(Editor sp) {
		sp.commit();
	}
}
